/**
 * Combo box menu class.
 *
 * @author Andrey Anisimov <hercules@inbox.ru>
 * @see core.ui.ComboBox
 */

import core.ui.Menu;
import core.ui.Window;
import core.ui.MenuItem;
import core.ui.ScrollWindow;

// TODO: add custom vertical scrollbar
class ComboBoxMenu extends Menu {
	
	/**
	 * Initiualizes the new combo box menu control.
	 */
	this.ComboBoxMenu = function() {
		super();
		// FIXME: Mozilla have bug with native scrollbar - 
		// they stealing input focus when hidden.
		this.setStyle('overflow-y', 'auto');
		this.setStyle('display', 'none');

		this.clickHandler = bind(this, this.onItemClick);
		this.mouseoverHandler = bind(this, this.onItemMouseOver);
		this.mouseoutHandler = bind(this, this.onItemMouseOut);
	}

	/**
	 * Adds a new item to the combo box menu.
	 * 
	 * @param text (string) the text displayed in the new item.
	 * @param value (variant) the new item's value.
	 * @param selected (bool) whether the new item is selected
	 */
	this.addItem = function(text, value, selected) {
		var item = new MenuItem(this);
		item.setStyleName('ComboBoxMenuItem');
		item.setStyle('overflow', 'hidden');
		item.setText(text);
		item.value = value;
		item.onmouseover = this.mouseoverHandler;
		item.onmouseout = this.mouseoutHandler;
		item.onclick = this.clickHandler;
		if (selected) {
			this.selectedItem = item;
		}
		return super.addItem(item);
	}
	
	/**
	 * Clears the menu.
	 */
	this.removeItems = function() {
		this.selectedItem = undefined;
		super.removeItems();
	}
	
	/**
	 * Sets the item selected.
	 * 
	 * @param item (MenuItem) item to set selected
	 */
	this.setSelectedItem = function(item) {
		if (this.selectedItem instanceof Window) {
			this.selectedItem.removeStyleName('ComboBoxMenuItem-selected');
		}
		item.addStyleName('ComboBoxMenuItem-selected');
		this.selectedItem = item;
	}
	
	/**
	 * Returns currently selected menu item. If no item is selected,
	 * the function returns undefined.
	 * 
	 * @return MenuItem
	 */
	this.getSelectedItem = function() {
		return this.selectedItem;
	}
	
	/**
	 * Displays the menu in popup state.
	 * 
	 * @param origin (Window) The window which cliking does not close the popup
	 * @param x, y (int) The popup position
	 */
	this.doPopup = function(origin, x, y) {
		if (this.selectedItem instanceof Window) {
			this.selectedItem.addStyleName('ComboBoxMenuItem-selected');
		}
		// FIXME: see comments about "overflow: auto" and "visibility: hidden"
		this.setStyle('display', '');
		super.doPopup(origin, x, y);
	}
	
	/**
	 * Ends popup state and hides the menu.
	 */
	this.endPopup = function() {
		super.endPopup();
		// FIXME: see comments about "overflow: auto" and "visibility: hidden"
		this.setStyle('display', 'none');
	}

	this.onItemMouseOver = function(event, item) {
		if (this.selectedItem instanceof Window) {
			this.selectedItem.removeStyleName('ComboBoxMenuItem-selected');
		}
		item.addStyleName('ComboBoxMenuItem-highlight');
	}
	
	this.onItemMouseOut = function(event, item) {
		item.removeStyleName('ComboBoxMenuItem-highlight');
	}
	
	this.onItemClick = function(event, item) {
		this.setSelectedItem(item);
		this.fireItemSelected();
	}
}